using System;
using System.Data;
using Framework.Library.Components;
using Framework.Module.CMS.Library.Definitions;
using Framework.Module.CMS.Library.Providers;
using Framework.Library;
using Framework.Library.Utilities;
using Framework.Module.CMS.Library.Zones;

namespace Framework.Module.CMS.Library.Distributions
{
	public class DistributionController
	{
		private static string key = "Framework.Portal.CMS.Content";

#region Back end
        public static int AddDistribution(DistributionInfo distribution)
		{
			return DataAccessProvider.Instance().AddDistribution(distribution);
		}
        public static int GetDistID(int ContentID)
        {
            return DataAccessProvider.Instance().GetDistID(ContentID);
        }
		public static void SetDistributionPriority(int distId, int priority)
		{
			DataAccessProvider.Instance().SetDistributionPriority(distId, priority);
		}
		public static void SetDistributionRank(int distId, int rank)
		{
			DataAccessProvider.Instance().SetDistributionRank(distId, rank);
		}
		public static void UpdateDistributionView(int distId, int number)
		{
			DataAccessProvider.Instance().UpdateDistributionView(distId, number);
		}
		public static void DeleteDistribution(int distId)
		{
			DataAccessProvider.Instance().DeleteDistribution(distId);
		}
		public static void DeleteDistribution(int contentId, int zoneId)
		{
			DataAccessProvider.Instance().DeleteDistribution(contentId, zoneId);
		}
        public static DistributionInfo GetDistribution(int distId)
        {
            return DataAccessProvider.Instance().GetDistribution(distId);
        }
        public static DataTable GetDistributedContents(int zoneId, int userid)
        {
            //string param = "GetDistributedContents" + zoneId.ToString() + "_" + userid.ToString() ;
            DataCaching data = new DataCaching();
            DataTable dtContents;//= (DataTable)data.GetHashCache(keycache, param);
            //if (dtContents != null) return dtContents;
            dtContents = DataAccessProvider.Instance().GetDistributedContents(zoneId, userid);
            //data.SetHashCache(keycache, param, Constants.CacheExpire_Content, dtContents);
            return dtContents;
        }
        public static DataTable GetDistributedContents(int zoneId)
        {
            //string param = "GetDistributedContents" + zoneId.ToString();
            // DataCaching data = new DataCaching();
            DataTable dtContents;//= (DataTable)data.GetHashCache(keycache, param);
            //if (dtContents != null) return dtContents;
            dtContents = DataAccessProvider.Instance().GetDistributedContents(zoneId);
            //data.SetHashCache(keycache, param, Constants.CacheExpire_Content, dtContents);
            return dtContents;
        }

        public static DataTable Distributions_GetNewContent(int recordnumber)
        {
            string keycache = key + "_" + AppEnv.PortalId();
            string param = "Distributions_GetNewContent_" + recordnumber.ToString();
            DataCaching data = new DataCaching();
            DataTable dtContents = (DataTable)data.GetHashCache(keycache, param);
            if (dtContents != null) return dtContents;
            dtContents = DataAccessProvider.Instance().Distributions_GetNewContent(recordnumber);
            data.SetHashCache(keycache, param, AppEnv.CacheExpire_Content, dtContents);
            return dtContents;
        }        

        public static bool RemoverInZoneID(int contentID, int zoneID)
        {
            return DataAccessProvider.Instance().RemoverInZoneID(contentID, zoneID);
        }
        public static void UpdatePostDate(DistributionInfo _distinfo)
        {
            DataAccessProvider.Instance().UpdatePostDate(_distinfo);
        }

        //dung beo moi them new
        public static DataTable Distributions_Search(int zoneid, int userid, string text, string lang, int portalid, DateTime fromdate, DateTime todate, string rank)
        {
            return DataAccessProvider.Instance().Distributions_Search(zoneid,  userid,  text,  lang,  portalid,  fromdate,  todate,  rank);
        }
#endregion
        

#region front end

                                                // Lay ra bai tai vi tri ben phai cua trang chu, cache theo zone va portal
        public static DataTable GetDistributedRightContents(int zoneId, int top, bool bol)
        {
            //string param = "GetDistributedRightContents" + AppEnv.PortalId() + "_" + zoneId.ToString() + "_" + top.ToString() + "_" + bol;
            ZoneInfo info = ZoneController.GetZone(zoneId);
            string keycache = key + "_" + AppEnv.PortalId() + "_" + UnicodeUtility.UnicodeToKoDau(info.Zone_Name);

            string param = "GetDistributedRightContents_";
            DataCaching data = new DataCaching();
            DataTable dtContents = null;
            dtContents = (DataTable)data.GetHashCache(keycache , param);
            
            if (dtContents == null)
            {
                dtContents = DataAccessProvider.Instance().GetDistributedRightContents(zoneId, top, bol);
                data.SetHashCache(keycache, param, AppEnv.CacheExpire_Content, dtContents);
            }
            return dtContents;
        }

                                                //lay ra bai noi bat trang chu, cache theo portal
        public static DataTable GetDistributedHotNewsContentsbyPortal(int portalid, int top)
        {
            string keycache = key + "_" + AppEnv.PortalId();
            string param = "GetDistributedHotNewsContentsbyPortal_" + top.ToString();
            DataCaching data = new DataCaching();            
            //data.SetHashCache(keycache, param, Constants.CacheExpire_Content, dtContents);
            DataTable dtContents = null;
            dtContents = (DataTable) data.GetHashCache(keycache, param);            
            if (dtContents == null)
            {
                dtContents = DataAccessProvider.Instance().GetDistributedHotNewsContentsbyPortal(portalid, top);
                data.SetHashCache(keycache, param, AppEnv.CacheExpire_Content, dtContents);
            }
            return dtContents;
        }
       
                                                // search cho nguoi truy cap
        public static DataTable SearchForWeb(int pagesize, int curpage, out int totalrecord, string text)
        {
            return DataAccessProvider.Instance().SearchForWeb(pagesize, curpage, out totalrecord, text);
        }

        public static DataTable SearchForWebAdvance(int pagesize, int curpage, out int totalrecord, string text, int zoneid)
        {
            return DataAccessProvider.Instance().SearchForWebAdvance(pagesize, curpage, out totalrecord, text, zoneid);
        }

                                                // Search cho website
        public static DataTable Search(int pagesize, int distid, out int totalrecord, string text, bool curpage)
        {
            return DataAccessProvider.Instance().Search(pagesize, distid, out totalrecord, text,curpage);
        }

                                                // lay ra bai cu hon so voi bai  hien tai
        public static DataTable GetOldDistributedContent(int curContentId, int numberRecord)
        {
            string keycache = key + "_" + AppEnv.PortalId();
            string param = "GetOldDistributedContent_" + curContentId.ToString() + "_" + numberRecord.ToString();
            DataCaching data = new DataCaching();
            DataTable dtContents = (DataTable)data.GetHashCache(keycache, param);
            if (dtContents != null) return dtContents;
            dtContents = DataAccessProvider.Instance().GetOldDistributedContent(curContentId, numberRecord);
            data.SetHashCache(keycache, param, AppEnv.CacheExpire_Content, dtContents);
            return dtContents;
        }

                                                //lay ra bai moi hon so voi bai hien tai
        public static DataTable GetNewDistributedContent(int curContentId, int numberRecord)
        {
            string keycache =  key + "_" + AppEnv.PortalId();
            string param = "GetNewDistributedContent_" + curContentId.ToString() + "_" + numberRecord.ToString();
            DataCaching data = new DataCaching();
            DataTable dtContents = (DataTable)data.GetHashCache(keycache, param);
            if (dtContents != null) return dtContents;
            dtContents = DataAccessProvider.Instance().GetNewDistributedContent(curContentId, numberRecord);
            data.SetHashCache(keycache, param, AppEnv.CacheExpire_Content, dtContents);
            return dtContents;
        }

                                                //lay ra bai viet chi trong 1 chuyen muc
        public static DataTable GetContentsBySeftZoneIDAndRank(int zoneId, int top, int rank)
        {
            ZoneInfo info = ZoneController.GetZone(zoneId);
            string keycache = key + "_" + AppEnv.PortalId() + "_" + UnicodeUtility.UnicodeToKoDau(info.Zone_Name);

            string param = top.ToString() + "_" + rank;
            DataCaching data = new DataCaching();
            DataTable dtContents = null;
            dtContents = (DataTable)data.GetHashCache(keycache, param);
            if (dtContents == null)
            {
                dtContents = DataAccessProvider.Instance().GetContentsBySeftZoneIDAndRank(zoneId, top, rank);
                data.SetHashCache(keycache, param, AppEnv.CacheExpire_Content, dtContents);
            }
            return dtContents;
        }

                                                // lay ra cac bai trong chuyen muc
        public static DataTable GetNewContentByZoneIDAndRank(int zoneID, bool inZone, int pageSize, int currentPage, out int totalRecord)
        {
            //Zones.ZoneInfo info = Zones.ZoneController.GetZone(zoneID);
            //string keycache = key + "_" + AppEnv.PortalId() + "_" + UnicodeUtility.UnicodeToKoDau(info.Zone_Name);

            //string param = currentPage.ToString();
            //DataCaching dataCaching = new DataCaching();
            //Object dtTotalrecord = dataCaching.GetHashCache(param, zoneID);
            //if (dtTotalrecord != null)
            //{
            //    totalRecord = (int)dtTotalrecord;
            //}
            //else
            //{
            //    totalRecord = 0;
            //}

            DataTable dtContents;// = (DataTable)dataCaching.GetHashCache(keycache, param);
            //if (dtContents != null) 
            //    return (DataTable)dtContents;           
            dtContents = DataAccessProvider.Instance().GetNewContentByZoneIDAndRank(zoneID, inZone, pageSize, currentPage, out totalRecord);
           // dataCaching.SetHashCache(keycache , param, Constants.CacheExpire_Content, dtContents);
            return dtContents;
        }

        public static DataTable GetDistributedContentsByZone(int zoneID)
        {
            DataTable dtContents;
            dtContents = DataAccessProvider.Instance().GetDistributedContentsByZone(zoneID);
            return dtContents;
        }
                                                // Lay ra cac bai theo chuyen muc va loai bai
        public static DataTable GetDistributedContents(int zoneId, int top, int rank)
        {
            string param = "GetDistributedContents_" + top.ToString() + "_" + rank;
            ZoneInfo info = ZoneController.GetZone(zoneId);
            string keycache = key + "_" + AppEnv.PortalId() + "_" + UnicodeUtility.UnicodeToKoDau(info.Zone_Name);
            DataCaching data = new DataCaching();
            DataTable dtContents = null;
            dtContents = (DataTable)data.GetHashCache(keycache, param);
            if (dtContents == null)
            {
                dtContents = DataAccessProvider.Instance().GetDistributedContents(zoneId, top, rank);
                data.SetHashCache(keycache, param, AppEnv.CacheExpire_Content, dtContents);
            }
            return dtContents;
        }

                                                // Lay ra cac bai doc nhieu nhat trong 1 kenh
        public static DataTable Distributions_GetContentByView(int recordnumber)
        {
            string keycache = key +  "_" + AppEnv.PortalId();
            string param ="Distributions_GetContentByView_" + recordnumber.ToString();
            DataCaching data = new DataCaching();
            DataTable dtContents = (DataTable)data.GetHashCache(keycache, param);
            if (dtContents == null)
            {
                dtContents = DataAccessProvider.Instance().Distributions_GetContentByView(recordnumber);
                data.SetHashCache(keycache, param, AppEnv.CacheExpire_Content, dtContents);
            }
            return dtContents;
        }
        public static DataTable Distributions_GetContentByView(int recordnumber, int portalid)
        {
            string keycache = key + "_" + portalid.ToString();
            string param = "Distributions_GetContentByView_" + recordnumber.ToString();
            DataCaching data = new DataCaching();
            DataTable dtContents = (DataTable)data.GetHashCache(keycache, param);
            if (dtContents == null)
            {
                dtContents = DataAccessProvider.Instance().Distributions_GetContentByView(recordnumber);
                data.SetHashCache(keycache, param, AppEnv.CacheExpire_Content, dtContents);
            }
            return dtContents;
        }
                                                // lay ra bai noi bat trang chu: hien tai ko su dung
        public static DataTable GetDistributedHotNewsContents(int zoneId, int top)
        {
            string keycache = key + "_" + AppEnv.PortalId();
            string param = "GetDistributedHotNewsContents_" + zoneId.ToString() + "_" + top.ToString();
            DataCaching data = new DataCaching();
            DataTable dtContents = null;
            dtContents = (DataTable)data.GetHashCache(keycache, param);
            if (dtContents == null)
            {
                dtContents = DataAccessProvider.Instance().GetDistributedHotNewsContents(zoneId, top);
                data.SetHashCache(keycache, param, AppEnv.CacheExpire_Content, dtContents);
            }
            return dtContents;
        }


        public static DataTable GetDistributedContents(int zoneId, int top, int rank, DateTime Date)
        {
            ZoneInfo info = ZoneController.GetZone(zoneId);
            string keycache = key + "_" + AppEnv.PortalId() + "_" + UnicodeUtility.UnicodeToKoDau(info.Zone_Name);

            string param = "GetDistributedContents_" + top.ToString() + "_" + rank + "_" + Date;
            DataCaching data = new DataCaching();
            DataTable dtContents = null;
            dtContents = (DataTable)data.GetHashCache(keycache, param);
            if (dtContents == null)
            {
                dtContents = DataAccessProvider.Instance().GetDistributedContents(zoneId, top, rank, Date);
                data.SetHashCache(keycache, param, AppEnv.CacheExpire_Content, dtContents);
            }
            return dtContents;
        }

        public static DataTable Distributions_GetPicInAlbumByContentId(int contentId)
        {
            DataTable dtContents;
            dtContents = DataAccessProvider.Instance().Distributions_GetPicInAlbumByContentId(contentId);
            return dtContents;
        }

#endregion
    }
}
